package com.xiaoshuidi.cloud.module.finance.convert.finance;

import com.alibaba.nacos.shaded.com.google.common.primitives.Doubles;
import com.xiaoshuidi.cloud.framework.yinlian.core.trade.vo.YinlianTransactionExcelVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.YinlianReconciliationDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Mapper
public interface YinlianConvert {
    YinlianConvert INSTANCE = Mappers.getMapper(YinlianConvert.class);
    static DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    default List<YinlianReconciliationDO> convert(List<YinlianTransactionExcelVO> data, String merchantCode) {
        if (data == null || data.size() == 0) {
            return new ArrayList<>(0);
        }
        List<YinlianReconciliationDO> list = new ArrayList<>(data.size());
        for (YinlianTransactionExcelVO d : data) {
            YinlianReconciliationDO dataDO = map(d);
            dataDO.setMerchantCode(merchantCode);
            dataDO.setTradeDate(LocalDateTime.parse(d.getTradeDate() + " " + d.getPayTime(), fmt));
            list.add(dataDO);
        }
        return list;
    }


    @Mapping(target = "tradeDate", ignore = true)
    YinlianReconciliationDO map(YinlianTransactionExcelVO excelVO);

    default BigDecimal map(String value) {
        if (StringUtils.hasText(value)) {
            return new BigDecimal(value.replaceAll(",", ""));
        }
        return null;
    }

    default Integer map2(String value) {
        if (StringUtils.hasText(value)) {
            return Integer.parseInt(value);
        }
        return null;
    }
}
